from fastapi import APIRouter,Request,HTTPException,Depends
import string
import random

from grpc import Status,StatusCode
from utils import status_code
from utils.aliysms import AliyunSMSSender
from schemas.response import ResponseModel, UpdatedAvatarModel
from utils.cache import TLLRedis
from schemas.request import AddressModel
from schemas.response import LoginedModel,UserListModel,UserModel,AddressResponseModel,AddressListModel

from utils.auth import AuthHandler
from services.user import UserServiceClient
from services.address import AddressServiceClient

from utils.status_code import get_http_code
from fastapi import UploadFile
from utils.alyoss import oss_upload_image

router=APIRouter(prefix='/address',tags=['地址'])

ttl_redis=TLLRedis()
auth_handler=AuthHandler()
address_service_client=AddressServiceClient()

@router.post('/add',response_model=AddressResponseModel)
async def add_address(params:AddressModel,user_id:int=Depends(auth_handler.auth_access_dependency)):

    return await address_service_client.CreateAddress(
        user_id=user_id,
        realname=params.realname,
        mobile=params.mobile,
        region=params.region,
        detail=params.detail,
        )

@router.delete('/delete/{id}',response_model=ResponseModel)
async def delete_address(id:str,user_id:int=Depends(auth_handler.auth_access_dependency)):
    await address_service_client.DeleteAddress(
        id=id,
        user_id=user_id,
        )
    return ResponseModel()

@router.get('/list',response_model=AddressListModel)
async def get_address_list(page:int=1,size:int=10,user_id:int=Depends(auth_handler.auth_access_dependency)):
    result= await address_service_client.GetAddressList(
        user_id=user_id,
        page=page,
        size=size,
        )
    address_list=[AddressResponseModel(
        id=item.id,
        realname=item.realname,
        mobile=item.mobile,
        region=item.region,
        detail=item.detail
    ) for item in result.address_list]
    total=result.total
    return AddressListModel(
        address_list=address_list,
        total=total,
        )

@router.get('/detail/{id}',response_model=AddressResponseModel)
async def get_address_detail(id:str,user_id:int=Depends(auth_handler.auth_access_dependency)):
    address= await address_service_client.GetAddressById(
        id=id,
        user_id=user_id,
        )
    if not address:
        raise HTTPException(status_code=get_http_code(StatusCode.NOT_FOUND),detail='地址不存在')
    return AddressResponseModel(
        id=address.id,
        realname=address.realname,
        mobile=address.mobile,
        region=address.region,
        detail=address.detail
    )
@router.put('/update/{id}',response_model=ResponseModel)
async def update_address(id:str,params:AddressModel,user_id:int=Depends(auth_handler.auth_access_dependency)):
    await address_service_client.UpdateAddress(
        address_id=id,
        realname=params.realname,
        mobile=params.mobile,
        region=params.region,
        detail=params.detail,
        user_id=user_id,
        )
    return ResponseModel()
